package com.lottery.service.pc28.wealthService.impl;

import com.alibaba.fastjson.JSON;
import com.lottery.dao.pc28.wealthDao.HistoryDao;
import com.lottery.dto.PageDto;
import com.lottery.dto.ResultDto;
import com.lottery.dto.VersionDto;
import com.lottery.entity.History;
import com.lottery.service.pc28.wealthService.HistoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HistoryServiceImpl implements HistoryService {
	@Resource
	private HistoryDao historyDao;
	// 日志
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 校验版本号
	 */
	@Override
	public String selectHistoryService(String StrVersion, String deviceType) {
		Map<String, Object> maxMap =new HashMap<>(); 
		VersionDto versionDto = new VersionDto();
		try {
			// 获取所有版本号对象
			List<History> list = historyDao.selectHistory(deviceType);
			if (list.isEmpty()) {
				versionDto.setMessage("当前版本是最新版本");
				versionDto.setStatus(0);
				maxMap.put("array", versionDto);
				maxMap.put("result", "1");
				maxMap.put("msg", "失败");
				/*resultDto.setData(versionDto);
				resultDto.setSuccess(false);*/
				return JSON.toJSONString(maxMap, true);
			}
			// 得到最新修改版本号对象
			History history = list.get(0);
			// 最新版本下载地址
			String url = history.getUrl();
			// 得到最新修改版本号
			String version = history.getVersion();
			logger.info("version: {}--{}", version, StrVersion);

			History h = new History();
			h.setVersion(StrVersion);
			h.setType(deviceType);
			History historyVersion = historyDao.selectHistoryByVersion(h);
			if (historyVersion == null) {
				versionDto.setMessage("当前版本不存在，请更新");
				versionDto.setUrl(history.getUrl());
				versionDto.setVersion(history.getVersion());
				versionDto.setRemark(history.getDepict());
				versionDto.setStatus(2);
				maxMap.put("array", versionDto);
				maxMap.put("result", "0");
				maxMap.put("msg", "成功");
				return JSON.toJSONString(maxMap, true);
			}
			int usable = historyVersion.getUsable();
			if (usable == 1) {
				versionDto.setUrl(url);
				versionDto.setVersion(history.getVersion());
				versionDto.setRemark(history.getDepict());
				versionDto.setStatus(2);// 0最新版本，1建议更新，2强制更新
				versionDto.setMessage("当前版本不可用，请更新");
				maxMap.put("array", versionDto);
				maxMap.put("result", "0");
				maxMap.put("msg", "成功");
				return JSON.toJSONString(maxMap, true);
			} else {
				if (version.equals(StrVersion)) {
					versionDto.setStatus(0);// 0最新版本，1建议更新，2强制更新
					versionDto.setMessage("当前版本是最新版本");
					versionDto.setVersion(history.getVersion());
					versionDto.setRemark(history.getDepict());
					maxMap.put("array", versionDto);
					maxMap.put("result", "0");
					maxMap.put("msg", "成功");
					return JSON.toJSONString(maxMap, true);
				} else {
					versionDto.setUrl(url);
					versionDto.setVersion(history.getVersion());
					versionDto.setRemark(history.getDepict());
					versionDto.setStatus(1);// 0最新版本，1建议更新，2强制更新
					versionDto.setMessage("当前版本不是最新版本，建议更新");
					maxMap.put("array", versionDto);
					maxMap.put("result", "0");
					maxMap.put("msg", "成功");
					return JSON.toJSONString(maxMap, true);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			maxMap.put("result", "1");
			maxMap.put("msg", "异常");
			return JSON.toJSONString(maxMap, true);
		}
	}

	/**
	 * 查询所有下载版本记录
	 * 
	 * @return
	 */
	public ResultDto<PageDto> selectAllHistory(int pageNow) {
		ResultDto<PageDto> resultDto = new ResultDto<PageDto>();
		PageDto pageDto = new PageDto();
		int pageSize = 10;
		int totalCount = 0;
		try {
			totalCount = historyDao.selectWebTptalCount();
		} catch (Exception e) {
			resultDto.setSuccess(false);
			return resultDto;
		}
		int totalPageCount = (totalCount + pageSize - 1) / pageSize;
		int startPage = (pageNow - 1) * pageSize;
		pageDto.setPageSize(pageSize);
		pageDto.setPageNow(pageNow);
		pageDto.setstartPage(startPage);
		pageDto.setTotalCount(totalCount);
		pageDto.setTotalPageCount(totalPageCount);
		resultDto.setData(pageDto);
		try {
			List<History> list = historyDao.selectWebHistory(pageDto);
			resultDto.setData(pageDto);
			resultDto.setList(list);
			resultDto.setSuccess(true);
		} catch (Exception e) {
			resultDto.setSuccess(false);
		}
		return resultDto;
	}

	/**
	 * 添加历史版本
	 * 
	 * @return
	 */
	public ResultDto<History> insertHistory(History history) {
		ResultDto<History> resultDto = new ResultDto<History>();
		try {
			historyDao.insertHistory(history);
			resultDto.setSuccess(true);
		} catch (Exception e) {
			resultDto.setSuccess(false);
		}
		return resultDto;
	}

	/**
	 * 修改版本管理
	 * 
	 * @return
	 */
	public ResultDto<History> updateHistory(History history) {
		ResultDto<History> resultDto = new ResultDto<History>();
		try {
			historyDao.updateHistory(history);
			resultDto.setSuccess(true);
		} catch (Exception e) {
			resultDto.setSuccess(false);
		}
		return resultDto;
	}
}
